<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="属性名" prop="attrName">
      <el-input v-model="dataForm.attrName" placeholder="属性名" />
    </el-form-item>
    <el-form-item label="是否需要检索" prop="searchType">
      <el-radio v-model="dataForm.searchType" :label="1" border>需要</el-radio>
      <el-radio v-model="dataForm.searchType" :label="0" border>不需要</el-radio>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标" />
    </el-form-item>
    <el-form-item label="可选值列表" prop="valueSelect">
      <el-input v-model="dataForm.valueSelect" placeholder="可选值列表" />
    </el-form-item>
    <el-form-item label="属性类型" prop="attrType">
      <el-radio v-model="dataForm.attrType" :label="1" border>基本属性</el-radio>
      <el-radio v-model="dataForm.attrType" :label="0" border>销售属性</el-radio>
    </el-form-item>
    <el-form-item label="启用状态" prop="enable">
      <el-switch
        v-model="dataForm.enable"
        active-color="#13ce66"
        inactive-color="#ff4949"
        :active-value="1"
        :inactive-value="0"
        active-text="启用"
        inactive-text="禁用"
      />
    </el-form-item>
    <el-form-item label="所属分类" prop="categoryId">
      <el-cascader v-model="cascadedCategoryId" :options="categories" :props="props" placeholder="请选择所属分类"/>
    </el-form-item>
    <el-form-item label="所属分组" prop="attrGroupId">
      <el-select ref="groupSelect" v-model="dataForm.attrGroupId" placeholder="请选择所属分组">
        <el-option
          v-for="item in attrGroups"
          :key="item.id"
          :label="item.name"
          :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="快速展示" prop="showDesc">
      <el-radio v-model="dataForm.showDesc" :label="1" border>是</el-radio>
      <el-radio v-model="dataForm.showDesc" :label="0" border>否</el-radio>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          attrId: 0,
          attrName: '',
          searchType: '',
          icon: '',
          valueSelect: '',
          attrType: '',
          enable: '',
          categoryId: '',
          showDesc: '',
          attrGroupId: ''
        },
        dataRule: {
          attrName: [
            { required: true, message: '属性名不能为空', trigger: 'blur' }
          ],
          searchType: [
            { required: true, message: '是否需要检索[0-不需要，1-需要]不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          valueSelect: [
            { required: true, message: '可选值列表[用逗号分隔]不能为空', trigger: 'blur' }
          ],
          attrType: [
            { required: true, message: '属性类型[0-销售属性，1-基本属性]不能为空', trigger: 'blur' }
          ],
          enable: [
            { required: true, message: '启用状态[0 - 禁用，1 - 启用]不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '所属分类不能为空', trigger: 'blur' }
          ],
          attrGroupId: [
            { required: true, message: '所属属性分组不能为空', trigger: 'blur' }
          ],
          showDesc: [
            { required: true, message: '快速展示【是否展示在介绍上；0-否 1-是不能为空', trigger: 'blur' }
          ]
        },
        cascadedCategoryId: [],
        props: {
          value: 'id',
          label: 'name',
          children: 'childrenCategories'
        },
        categories: [],
        attrGroups: []
      }
    },
    created () {
      this.getCascadeData()
    },
    watch: {
      cascadedCategoryId (path) {
        this.attrGroups = []
        this.dataForm.categoryId = path[path.length - 1]
        if (path && path.length === 3) {
          this.$http({
            url: this.$http.adornUrl(`/commodity/attr/list/${path[path.length - 1]}`),
            method: 'get',
            params: this.$http.adornParams({ page: 1, limit: 10000000 })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.attrGroups = data.page.list
            } else {
              this.$message.error(data.msg)
            }
          })
        } else if (path.length === 0) {
          this.dataForm.categoryId = ''
        } else {
          this.$message.error('请选择正确的分类')
          this.dataForm.categoryId = ''
        }
      }
    },
    methods: {
      init (id) {
        this.cascadedCategoryId = []

        this.dataForm.attrId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrId) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/at/info/${this.dataForm.attrId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.attrName = data.at.attrName
                this.dataForm.searchType = data.at.searchType
                this.dataForm.icon = data.at.icon
                this.dataForm.valueSelect = data.at.valueSelect
                this.dataForm.attrType = data.at.attrType
                this.dataForm.enable = data.at.enable
                this.dataForm.categoryId = data.at.categoryId
                this.dataForm.showDesc = data.at.showDesc
                this.dataForm.attrGroupId = data.at.attrGroupId
                this.cascadedCategoryId = data.at.cascadedCategoryId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.dataForm.categoryId = this.cascadedCategoryId[this.cascadedCategoryId.length - 1]
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/at/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrId': this.dataForm.attrId || undefined,
                'attrName': this.dataForm.attrName,
                'searchType': this.dataForm.searchType,
                'icon': this.dataForm.icon,
                'valueSelect': this.dataForm.valueSelect,
                'attrType': this.dataForm.attrType,
                'enable': this.dataForm.enable,
                'categoryId': this.dataForm.categoryId,
                'showDesc': this.dataForm.showDesc,
                'attrGroupId': this.dataForm.attrGroupId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getCascadeData () {
        this.$http({
          url: this.$http.adornUrl('/commodity/category/list/tree'),
          method: 'get'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.categories = data.data
          }
        })
      }
    }
  }
</script>
